<?php
// ================curl get post 请求数据==========================
// 一：GET请求，等同于    file_get_contents($url);
function get_url($url){
	$curl = curl_init($url);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_TIMEOUT, 500);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($curl, CURLOPT_URL, $url);
	$res = curl_exec($curl);
	curl_close($curl);
	return $res;	
}

//一： post 发送数据
public function post_url($url,$data=''){
	$options = array(  
	    CURLOPT_RETURNTRANSFER => true,  
	    CURLOPT_HEADER         => false,  
	    CURLOPT_POST           => true,  
	    CURLOPT_POSTFIELDS     => $data,  
	);
	$ch = curl_init($url);  
	curl_setopt_array($ch, $options);  
	$result = curl_exec($ch);  
	curl_close($ch);  
	return $result;  
}

//二： post 发送数据
function http_request($url, $data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
}

// 现在网址的url
public function now_url($bool = false){
     $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";	
    if($bool == false){
        $redirect_uri = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";            // 当前 URL 完整的.        
    }else{
        $redirect_uri = "$protocol$_SERVER[HTTP_HOST]";      // 域名访问
    }
    return $redirect_uri;
}



function simpleRequest( $url , $post_data = array() ,$option=array())
{
        //使用http_build_query拼接post
        if ( '' == $url )
        {
                return false;
        }
        $url_ary = parse_url( $url );
        if ( !isset( $url_ary['host'] ) )
        {
                return false;
        }
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true);
    //curl_setopt($ch, CURLOPT_ENCODING ,'gzip'); //加入gzip解析

        curl_setopt( $ch, CURLOPT_HEADER, ($option['CURLOPT_HEADER']===true) );
        if($option['referer']!='')
        {
                curl_setopt( $ch, CURLOPT_REFERER, $option['referer']);
        }
        if(!empty($post_data))
        {
                curl_setopt( $ch, CURLOPT_POST, true);
                curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_data);
        }
        curl_setopt( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 6.00; Windows NT 5.0)' );

        $http_header = array();
        $http_header[] = 'Connection: close';
        $http_header[] = 'Pragma: no-cache';
        $http_header[] = 'Cache-Control: no-cache';
        $http_header[] = 'Accept: */*';
        if(isset($option['header']))
        {
                foreach($option['header'] as $header)
                {
                        $http_header[] = $header;
                }
        }
        curl_setopt( $ch, CURLOPT_HTTPHEADER, $http_header );

        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        if ( !isset($option['timeout']))
        {
                $option['timeout'] = 10;
        }

        curl_setopt( $ch, CURLOPT_TIMEOUT, $option['timeout'] );
        $result = curl_exec( $ch );
        curl_close( $ch );
        return $result;
}




